package v1

import (
	"adminHome/models"
	"adminHome/pkg/app"
	"adminHome/pkg/e"
	"adminHome/pkg/util"
	"encoding/json"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

func AddInOrder(c *gin.Context) {
	appG := app.Gin{c}
	var order_detail []map[string]interface{}
	detail := c.PostForm("order_detail")
	storageId := com.StrTo(c.PostForm("storage_id")).MustInt()
	totalPrice := com.StrTo(c.PostForm("price")).MustFloat64()
	totalNum := com.StrTo(c.PostForm("num")).MustInt()
	itype := com.StrTo(c.PostForm("type")).MustInt()
	remark := c.PostForm("remark")

	json.Unmarshal([]byte(detail), &order_detail)
	valid := validation.Validation{}
	valid.Required(storageId, "storage_id").Message("仓库为必填")
	valid.Required(totalNum, "total_num").Message("总数为必填")
	valid.Required(totalPrice, "total_price").Message("总价为必填")
	valid.Required(itype, "type").Message("类型为必填")
	valid.Required(detail, "order_detail").Message("产品数据为必填")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}
	storage, _ := models.GetStorageById(storageId)

	mainData := make(map[string]interface{})
	mainData["storage_id"] = storageId
	mainData["storage_name"] = storage.Name
	mainData["type"] = itype
	mainData["total_price"] = totalPrice
	mainData["total_num"] = totalNum
	mainData["remark"] = remark
	mainData["code"] = util.GenerateCode()
	mainData["create_user"] = "admin"

	mainId := models.AddInOrder(mainData)
	for _, v := range order_detail {
		v["num"] = com.StrTo(v["num"].(string)).MustInt()
		v["product_id"] = int(v["product_id"].(float64))
		models.AddInOrderDetail(v, mainId)
	}
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, mainId)
}

/**
审核入库单成功
*/
func EditInOrder(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	totalPrice := com.StrTo(c.PostForm("total_price")).MustFloat64()
	totalNum := com.StrTo(c.PostForm("total_num")).MustInt()
	remark := c.PostForm("remark")

	valid := validation.Validation{}
	valid.Required(id, "id").Message("id为必填")
	valid.Required(totalPrice, "total_price").Message("总价为必填")
	valid.Required(totalNum, "total_num").Message("总数量为必填")

	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistInOrderById(id) {
		code = e.ERROR_NOT_EXIST_INORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	mainData := make(map[string]interface{})
	mainData["total_price"] = totalPrice
	mainData["total_num"] = totalNum
	mainData["remark"] = remark

	if models.EditInOrder(id, mainData) {
		code = e.ERROR_UPDATE_INORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}

func GetInOrderList(c *gin.Context) {
	appG := app.Gin{c}
	data := make(map[string]interface{})
	response := make(map[string]interface{})

	code_ := c.Query("code")
	storageId := com.StrTo(c.DefaultQuery("storage_id", "0")).MustInt()

	if code_ != "" {
		data["code"] = code_
	}
	if storageId != 0 {
		data["storage_id"] = storageId
	}
	inOrder, _ := models.GetInOrderList(util.GetPage(c), util.GetPageSize(c), data)
	total, _ := models.GetInOrderTotal(data)

	response["list"] = inOrder
	response["total"] = total
	code := e.SUCCESS
	appG.Response(http.StatusOK, code, response)
}

func GetInOrder(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistInOrderById(id) {
		code = e.ERROR_NOT_EXIST_INORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}
	inOrder, _ := models.GetOrderById(id)
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, inOrder)
}

func DeleteInOrder(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Required(id, "id").Message("id不能为空")
	code := e.INVALID_PARAMS
	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusOK, code, nil)
		return
	}

	if !models.ExistInOrderById(id) {
		code = e.ERROR_NOT_EXIST_INORDER
		appG.Response(http.StatusOK, code, nil)
		return
	}

	models.DeleteInOrder(id)
	code = e.SUCCESS
	appG.Response(http.StatusOK, code, nil)
}
